local s15__jijiang = fk.CreateSkill {
  name = "s15__jijiang",
}

Fk:loadTranslationTable {
  ["s15__jijiang"] = "激将",
  [":s15__jijiang"] = "若你为主帅，出牌阶段限一次，你可以选择一名敌方角色，令你的前锋选择是否打出一张【杀】，来视为你使用一张无距离限制的普通【杀】。",
  ["#s15__jijiang-choose"] = "激将：选择一名敌方角色作为目标",
  ["#s15__jijiang-ask"] = "激将：是否打出一张【杀】视为 %src 使用【杀】？",
}

s15__jijiang:addEffect("viewas", {
  anim_type = "offensive",
  pattern = "slash",
  card_filter = Util.FalseFunc,
  view_as = function(self, player, cards)
    if #cards ~= 0 then return end
    -- 检查是否为主帅（根据座位号判定）
    if player.seat ~= 2 and player.seat ~= 5 then return end
    -- 检查使用次数
    if player:getMark("s15__jijiang_used-phase") > 0 then return end

    local c = Fk:cloneCard("slash")
    c.skillName = s15__jijiang.name
    return c
  end,
  before_use = function(self, player, use)
    local room = player.room
    if use.tos then
      room:doIndicate(player.id, table.map(use.tos, Util.IdMapper))
    end

    local vanguards = {}
    -- 获取所有存活的友方前锋
    for _, p in ipairs(room.alive_players) do
      if p:isFriend(player) and p ~= player then
        table.insert(vanguards, p)
      end
    end

    -- 询问所有己方前锋
    for _, p in ipairs(vanguards) do
      local respond = room:askToResponse(p, {
        skill_name = s15__jijiang.name,
        pattern = "slash",
        prompt = "#s15__jijiang-ask:" .. player.id,
        cancelable = true,
      })
      if respond then
        respond.skipDrop = true
        room:responseCard(respond)

        use.card = respond.card
        use.extra_data = { real_card = respond.card }
        return
      end
    end

    -- 没有前锋响应，标记使用次数
    room:addPlayerMark(player, "s15__jijiang_used-phase", 1)
    return s15__jijiang.name
  end,
  enabled_at_play = function(self, player)
    -- 检查是否为主帅（根据座位号判定）
    if player.seat ~= 2 and player.seat ~= 5 then return false end
    -- 检查使用次数
    if player:getMark("s15__jijiang_used-phase") > 0 then return false end

    local room = Fk:currentRoom()
    -- 检查是否有存活的友方前锋
    local vanguards = {}
    for _, p in ipairs(room.alive_players) do
      if p:isFriend(player) and p ~= player then
        table.insert(vanguards, p)
      end
    end

    return #vanguards > 0
  end,
  enabled_at_response = function(self, player)
    -- 检查是否为主帅（根据座位号判定）
    if player.seat ~= 2 and player.seat ~= 5 then return false end

    local room = Fk:currentRoom()
    -- 检查是否有存活的友方前锋
    local vanguards = {}
    for _, p in ipairs(room.alive_players) do
      if p:isFriend(player) and p ~= player then
        table.insert(vanguards, p)
      end
    end

    return #vanguards > 0
  end,
})

return s15__jijiang
